import { createRouter, createWebHashHistory } from 'vue-router'
import {localGet, isTest} from "../utils/index.js";
const router = createRouter({
  history: createWebHashHistory(), // hash模式：createWebHashHistory，history模式：createWebHistory
  routes: [
    {
      path: '/',
      redirect: '/introduce'
    },
    {
      path: '/introduce',
      name: 'introduce',
      component: () => import( '../views/systemMang/Introduce.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('../views/Login.vue')
    },
    {
      path: '/forum',
      name: 'forum',
      component: () => import('../views/Forum.vue')
    },
    {
      path: '/entries',
      name: 'entries',
      component: () => import('../views/entries.vue')
    },
    {
      path: '/showData',
      name: 'showData',
      component: () => import('../views/systemMang/showData.vue')
    },
    {
      path:'/userInfo',
      name: 'userInfo',
      component: () => import('../views/userMang/userInfo.vue')
    },
    {
      path: '/products',
      name: 'products',
      component: () => import('../views/products.vue')
    },
    {
      path: '/orders',
      name: 'orders',
      component: () => import('../views/orders.vue')
    },
  ]
})
//路由跳转页面之前
router.beforeEach((to, from ,next) => {
  const whiteList = ["/login", "/reg"] ;//白名单地址
  //sessionStorage,localStorage
  //从本地会话存储中获取登录凭证token
  const token = localGet("token");
  console.log(1111, isTest)
  //如果有凭证或者在请求白名单中，就正常往后面跳转页面
  if(token || whiteList.includes(to.path) || isTest){
      console.log("next")
    next(); //征程跳转到目标地址
  }
  else{
    next("/login");//未登陆，跳转到登录页
  }
})

export default router
